package org.example.controller;

import lombok.AllArgsConstructor;
import org.example.dao.Result;
import org.example.enums.responseEnum;
import org.example.service.FileUploadService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@AllArgsConstructor
@RestController
@RequestMapping("/upload")
public class FileUploadController {

    private final FileUploadService fileUploadService;


    @PostMapping("/headImage")
    Result updateHeadImage(@RequestParam("image") MultipartFile image, @RequestParam("id") Integer id) {
        responseEnum upload = fileUploadService.uploadHeadImage(image, id);
        if (upload == responseEnum.success) {
            return new Result(200, "上传成功", null);
        } else if (upload == responseEnum.error) {
            return new Result(400, "上传图片过大", null);
        } else {
            return new Result(500, "上传失败", null);
        }
    }


    @PostMapping("/dynamic")
    Result uploadDynamic(@RequestParam("image") MultipartFile images, @RequestParam("id") Integer id) {
        responseEnum upload = fileUploadService.uploadDynamicImage(images, id);
        if (upload == responseEnum.success) {
            return new Result(200, "上传成功", null);
        } else if (upload == responseEnum.error) {
            return new Result(400, "上传图片过大", null);
        } else {
            return new Result(500, "上传失败", null);
        }
    }
}
